/*******************************************************************************
 * Copyright (c) 2013,  Paul Daniels
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.hupu.jrs.components.jockeyjs

import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Binder
import android.os.IBinder
import android.webkit.WebView
import android.webkit.WebViewClient

class JockeyService : Service(), Jockey {

    private val _binder = JockeyBinder()

    private val _jockeyImpl = JockeyImpl.default

    override fun onBind(arg0: Intent): IBinder? {
        return _binder
    }

    inner class JockeyBinder : Binder() {

        val service: Jockey
            get() = this@JockeyService
    }

    override fun setOnValidateListener(listener: Jockey.OnValidateListener) {
        _jockeyImpl.setOnValidateListener(listener)
    }

    override fun on(type: String, vararg handler: JockeyHandler) {
        _jockeyImpl.on(type, *handler)
    }

    override fun off(type: String) {
        _jockeyImpl.off(type)
    }

    override fun send(type: String, toWebView: WebView) {
        send(type, toWebView, null!!)
    }

    override fun send(type: String, toWebView: WebView, withPayload: Any) {
        send(type, toWebView, withPayload, null!!)
    }

    override fun send(type: String, toWebView: WebView, complete: JockeyCallback) {
        send(type, toWebView, null!!, complete)
    }

    override fun send(type: String, toWebView: WebView, withPayload: Any, complete: JockeyCallback) {
        _jockeyImpl.send(type, toWebView, withPayload, complete)
    }

    override fun triggerCallbackOnWebView(webView: WebView, messageId: Int) {
        _jockeyImpl.triggerCallbackOnWebView(webView, messageId)
    }

    override fun configure(webView: WebView) {
        _jockeyImpl.configure(webView)
    }

    override fun handles(eventName: String): Boolean {
        return _jockeyImpl.handles(eventName)
    }

    override fun setWebViewClient(client: WebViewClient) {
        _jockeyImpl.setWebViewClient(client)
    }

    companion object {

        /**
         * Convenience method for binding to the JockeyService
         */
        fun bind(context: Context, connection: ServiceConnection): Boolean {
            return context.bindService(Intent(context, JockeyService::class.java), connection,
                    Context.BIND_AUTO_CREATE)
        }

        fun unbind(context: Context, connection: ServiceConnection) {
            context.unbindService(connection)
        }
    }
}
